package bar.dark.lee.setting.sms;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SMSReceiver extends BroadcastReceiver 
{
	static final String logTag = "SMSReceiver";
	  
//	private ArrayList<PaySMSData> mArrayPaySMSData = new ArrayList<PaySMSData>();;
	private String address2;//address1;
	private String body1;
//	private DBSQLite sql;
	private Context context;
	private Intent intent;
//	private String messageCardName;
//	private int parserNumber = -1;
//	private String linkCardName;
//	private String linkCardType;
//	
//	private int flag = 1;	// 번호와 내용이 1번컬럼인지 2번컬럼인지(1번:dispalyoriginal... 2번:그냥)
//	
//	private int cardCount;
//	
//	private int smsType = -1;
//	
//	private NotificationManager nm = null;
	
	@Override
	public void onReceive(Context ctx, Intent intent) {
		
		context = ctx;
		this.intent = intent;
		
		//abortBroadcast();
		
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int question = pref.getInt("smsRadioGroup", 1);
		
		if (question == 1)
		{
			return;
		}
		
		Bundle bundle = SMSReceiver.this.intent.getExtras();
		if (bundle == null) {
			return;
		}
		
		Object[] pdusObj = (Object[]) bundle.get("pdus");
		if (pdusObj == null) {
			return;
		}
		
		SmsMessage[] smsMessages = new SmsMessage[pdusObj.length];
		for (int i = 0; i < pdusObj.length; i++) {
			smsMessages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);

			//address1 = smsMessages[i].getOriginatingAddress();
			address2 = smsMessages[i].getDisplayOriginatingAddress();
			
			body1 = smsMessages[i].getDisplayMessageBody();
		}
		
		
		Intent inputIntent = new Intent(context, SmsInputService.class);
		inputIntent.putExtra("sms_address", address2);
		inputIntent.putExtra("sms_body", body1);
		
        context.startService(inputIntent);
	}

//	private boolean returnFlag = false;
//	/**
//	 * 결제문자정보 데이터 가져온다
//	 */
//	private boolean getPaySMSData(Context context, int flag) {
//		
//		sql = DBSQLite.getInstance(context);
//		
//		String query = "";
//		
//		어퍼스트로피 처리 해야함
//		if ((flag == 1) && (address1 != null) && !(address1.equalsIgnoreCase("")))
//		{
//			query = "SELECT messagecardname, linkcardname, linkcardtype, parser FROM sms where address='"+address1+"' and messagecardname ='"+messageCardName+"';";
//		}
////		else if ((flag == 2) && (address2 != null) && !(address2.equalsIgnoreCase("")))
////		{
////			query = "SELECT messagecardname, linkcardname, linkcardtype, parser FROM sms where address='"+address2+"' and messagecardname ='"+messageCardName+"';";
////		}
//		else
//		{
//			returnFlag = false;
//			return returnFlag;
//		}
//		
//		try {
//			sql.selectMapTile(context, query, new ParseSelectData()
//			{
//				@Override
//				public void resultData(Cursor c)
//				{
//					//Log.e("1", "resultData");
//					int count = c.getCount();
//					c.moveToFirst();
//					
//					if (count != 0)
//					{
//						//int addressIndex = c.getColumnIndex("address");
//						//int nameIndex = c.getColumnIndex("name");
//						int messageCardNameIndex = c.getColumnIndex("messagecardname");
//						int parserIndex = c.getColumnIndex("parser");
//						int linkCardNameIndex = c.getColumnIndex("linkcardname");
//						int linkCardTypeIndex = c.getColumnIndex("linkcardtype");
//						
//						for (int i = 0; i < count; i++)
//						{
//							if (messageCardName.equals(c.getString(messageCardNameIndex))) 
//							{
//								parserNumber = Integer.parseInt(c.getString(parserIndex));
//								linkCardName = c.getString(linkCardNameIndex);
//								linkCardType = c.getString(linkCardTypeIndex);
////								Log.e(logTag, "messageCardName : "+messageCardName);
////								Log.e(logTag, "parser : "+parserNumber);
////								Log.e(logTag, "linkCardName : "+linkCardName);
////								Log.e(logTag, "linkCardType : "+linkCardType);
//							}
//							
//							c.moveToNext();
//						}
//						
//						returnFlag = true;
//					}
//					else
//					{
//						returnFlag = false;			
//					}
//				}
//			});
//		} catch (Exception e) {
//			e.printStackTrace();
//			returnFlag = false;
//			
//			return returnFlag;
//		}
//		
//		return returnFlag;
//	}
}
